<template>
    <view>
        <navBar title="案卷" bgColor="#ededed"></navBar>
        <view class="list" v-for="(item, index) in pageData" :key="index">
            <view class="userInfo">
                <view class="User">
                    <image :src="imgUrls(item.avatar)" mode="aspectFill" class="HeadImg"></image>
                    <text class="User_name ellipsis_1">{{ item.name }}</text>
                </view>
                <text class="Datatime">{{ tampToDate(item.timestamp, true) }}</text>
            </view>

            <view class="content" @click="opendFile(item)">
                <image
                    :src="staticImg(getFileExtension(item.extend.path))"
                    mode="aspectFill"
                    class="fileImg"></image>
                <view class="box">
                    <text class="title ellipsis_1">{{ item.extend.name }}</text>
                    <text class="FilesSize"
                        >{{ getFileExtension(item.extend.path).split('.')[0] }}
                        {{ item.extend.size }}</text
                    >
                </view>
            </view>
        </view>

        <up-empty
            mode="list"
            marginTop="100"
            text="暂无相关内容"
            v-if="!pageData.length"></up-empty>
    </view>
</template>

<script setup>
    import { ref, computed, watch, nextTick, onUnmounted } from 'vue';
    import { onLoad, onShow, onReady, onReachBottom } from '@dcloudio/uni-app';
    import Http from '@/utils/request.js';
    import { useMainStore } from '@/stores/index';
    import {
        toUrl,
        staticImg,
        imgUrls,
        Toast,
        getFileExtension,
        tampToDate,
        downFile,
        openDocument,
    } from '@/utils/utils.js';

    const mainStore = useMainStore();
    const pageData = ref([]);
    const chatId = ref('');
    const size = ref(10);
    const current = ref(1);
    const getMore = ref(true);
    onLoad((opt) => {
        if (opt.chatId) chatId.value = opt.chatId;
        messagesPage();
    });

    const messagesPage = () => {
        Http.get({
            url: '/api/sys/messages/page',
            data: {
                searchText: '',
                fromId: '',
                chatId: chatId.value,
                messageType: 2,
                chatType: 1,
                size: size.value,
                current: current.value,
                existCurrent: 1, // 包含这次连接的记录 1包含 0不包含,默认不包含
            },
        }).then((res) => {
            if (res.code != 0) return Toast(res.msg);
            const result = res.data.records || [];
            current.value == 1
                ? (pageData.value = result)
                : (pageData.value = [...pageData.value, ...result]);
            if (result.length < size.value) getMore.value = false;
        });
    };

    const opendFile = async (Item) => {
        if (!Item.extend.path) return Toast('无法打开');
        const filePath = await downFile(Item.extend.path);
        openDocument(filePath);
    };

    onReachBottom(() => {
        if (!getMore.value) return;
        current.value++;
        messagesPage();
    });
</script>
<style lang="scss">
    page {
        padding-bottom: 30rpx;
    }
</style>
<style lang="scss" scoped>
    .list {
        width: 710rpx;
        padding: 20rpx;
        border-bottom: 1px solid #f7f7f7;
        margin: 20rpx auto 0;
        display: flex;
        flex-direction: column;
        .userInfo {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .User {
                flex: 1;
                display: flex;
                align-items: center;
                .HeadImg {
                    width: 60rpx;
                    height: 60rpx;
                    margin-right: 20rpx;
                }
                .User_name {
                    color: #000;
                    font-size: 30rpx;
                }
            }
            .Datatime {
                color: #999;
                font-size: 26rpx;
            }
        }
        .content {
            width: 100%;
            padding: 20rpx;
            margin-top: 20rpx;
            background-color: #f7f7f7;
            border-radius: 10rpx;
            display: flex;
            align-items: center;
            .fileImg {
                width: 100rpx;
                height: 100rpx;
                margin-right: 20rpx;
            }
            .box {
                flex: 1;
                height: 100rpx;
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                .title {
                    color: #000;
                    font-size: 30rpx;
                }
                .FilesSize {
                    color: #999;
                    font-size: 24rpx;
                }
            }
        }
    }
</style>
